/***
 * 根据线性数据生成树形数据
 * @source 源数组
 * @id 树id-字段名
 * @parentId 树父级id-字段名
 * @children 生成子数组-字段名
 * @fatherId 第一层的parentId
 * @returns {Array} 返回树形数据
 */
export const getTreeData = (source, id = 'id', parentId = 'parentId', children = 'children', fatherId = 0) => {

  let cloneData = JSON.parse(JSON.stringify(source)) // 深拷贝
  return cloneData.filter(father => {
    let branchArr = cloneData.filter(child => father[id] == child[parentId]);
    // branchArr.length > 0 ? father[children] = branchArr : null
    if (branchArr.length) {
      father[children] = branchArr
    }
    return father[parentId] == fatherId // 如果第一层不是parentId=0，请自行修改
  })
}